/**
 * WebSocket客户端示例
 * 用于与后端进行实时通信
 */

// 生成唯一的会话ID
function generateSessionId() {
    return 'session-' + Math.random().toString(36).substr(2, 9);
}

// WebSocket客户端类
class RobotWebSocketClient {
    constructor(baseUrl) {
        this.baseUrl = baseUrl || 'http://localhost:8080/iff';
        this.sessionId = generateSessionId();
        this.stompClient = null;
        this.connected = false;
        this.onStatusCallback = null;
        this.onResponseCallback = null;
        this.onChatCallback = null;
        this.onErrorCallback = null;
    }

    // 连接WebSocket
    connect() {
        const socket = new SockJS(`${this.baseUrl}/ws`);
        this.stompClient = Stomp.over(socket);
        
        // 禁用调试日志
        this.stompClient.debug = null;
        
        const that = this;
        
        this.stompClient.connect({}, function (frame) {
            console.log('WebSocket连接成功: ' + frame);
            that.connected = true;
            
            // 订阅状态更新
            that.stompClient.subscribe(`/topic/status/${that.sessionId}`, function (message) {
                const statusUpdate = JSON.parse(message.body);
                console.log('状态更新:', statusUpdate);
                if (that.onStatusCallback) {
                    that.onStatusCallback(statusUpdate);
                }
            });
            
            // 订阅查询响应
            that.stompClient.subscribe(`/topic/response/${that.sessionId}`, function (message) {
                const response = JSON.parse(message.body);
                console.log('查询响应:', response);
                if (that.onResponseCallback) {
                    that.onResponseCallback(response);
                }
            });
            
            // 订阅聊天响应
            that.stompClient.subscribe(`/topic/chat/${that.sessionId}`, function (message) {
                const chatResponse = JSON.parse(message.body);
                console.log('聊天响应:', chatResponse);
                if (that.onChatCallback) {
                    that.onChatCallback(chatResponse);
                }
            });
            
        }, function (error) {
            console.error('WebSocket连接错误:', error);
            that.connected = false;
            if (that.onErrorCallback) {
                that.onErrorCallback(error);
            }
        });
    }
    
    // 断开WebSocket连接
    disconnect() {
        if (this.stompClient && this.connected) {
            this.stompClient.disconnect();
            this.connected = false;
            console.log('WebSocket连接已断开');
        }
    }
    
    // 发送查询
    sendQuery(query) {
        if (!this.connected) {
            console.error('WebSocket未连接');
            return;
        }
        
        const request = {
            query: query,
            sessionId: this.sessionId
        };
        
        this.stompClient.send("/app/query", {}, JSON.stringify(request));
        console.log('查询已发送:', query);
    }
    
    // 发送聊天消息
    sendChat(message) {
        if (!this.connected) {
            console.error('WebSocket未连接');
            return;
        }
        
        const request = {
            message: message,
            sessionId: this.sessionId
        };
        
        this.stompClient.send("/app/chat", {}, JSON.stringify(request));
        console.log('聊天消息已发送:', message);
    }
    
    // 注册状态更新回调
    onStatus(callback) {
        this.onStatusCallback = callback;
    }
    
    // 注册查询响应回调
    onResponse(callback) {
        this.onResponseCallback = callback;
    }
    
    // 注册聊天响应回调
    onChat(callback) {
        this.onChatCallback = callback;
    }
    
    // 注册错误回调
    onError(callback) {
        this.onErrorCallback = callback;
    }
}

// 使用示例
/*
document.addEventListener('DOMContentLoaded', function() {
    // 创建WebSocket客户端
    const client = new RobotWebSocketClient('http://localhost:8080/iff');
    
    // 连接WebSocket
    client.connect();
    
    // 注册回调
    client.onStatus(function(status) {
        // 显示状态更新
        console.log('状态:', status.status);
    });
    
    client.onResponse(function(response) {
        // 显示查询结果
        console.log('响应:', response.response);
    });
    
    client.onChat(function(chatResponse) {
        // 显示聊天结果
        console.log('聊天响应:', chatResponse.response);
    });
    
    client.onError(function(error) {
        // 显示错误
        console.error('错误:', error);
    });
    
    // 按钮事件监听
    document.getElementById('queryButton').addEventListener('click', function() {
        const query = document.getElementById('queryInput').value;
        if (query) {
            client.sendQuery(query);
        }
    });
    
    document.getElementById('chatButton').addEventListener('click', function() {
        const message = document.getElementById('chatInput').value;
        if (message) {
            client.sendChat(message);
        }
    });
});
*/ 